// pages/inpatient-recharge/inpatient-recharge.js
Page({
  data: {
    inpatientInfo: null, // 住院人信息
    inpatientBalance: 60.00, // 住院余额
    selectedAmount: 0, // 初始未选择任何金额
    isCustomAmount: false, // 是否为自定义金额
    showPaymentModal: false, // 是否显示支付密码模态框
    paymentPassword: '', // 支付密码
    paymentAmount: 0, // 支付金额
    paymentPurpose: '住院充值', // 支付用途
    // 充值金额选项
    amountOptions: [500, 1000, 2000, 5000, 10000]
  },

  onLoad(options) {
    console.log('住院充值页面加载', options);
    
    // 接收传递的住院人信息
    if (options.inpatientId && options.inpatientName && options.inpatientNumber) {
      this.setData({
        inpatientInfo: {
          id: options.inpatientId,
          name: options.inpatientName,
          inpatientNumber: options.inpatientNumber
        }
      });
    }
  },

  // 选择充值金额
  selectAmount(e) {
    const amount = parseInt(e.currentTarget.dataset.amount);
    console.log('选择充值金额:', amount);
    
    this.setData({
      selectedAmount: amount,
      isCustomAmount: false
    });
  },

  // 选择自定义金额
  selectCustomAmount() {
    console.log('选择自定义金额');
    
    wx.showModal({
      title: '自定义金额',
      content: '请输入充值金额',
      editable: true,
      placeholderText: '请输入金额',
      success: (res) => {
        if (res.confirm && res.content) {
          const customAmount = parseFloat(res.content);
          if (customAmount > 0 && customAmount <= 50000) {
            this.setData({
              selectedAmount: customAmount,
              isCustomAmount: true
            });
            wx.showToast({
              title: `已选择${customAmount}元`,
              icon: 'success',
              duration: 1500
            });
          } else {
            wx.showToast({
              title: '请输入1-50000元之间的金额',
              icon: 'error',
              duration: 1500
            });
          }
        }
      }
    });
  },

  // 确认充值
  confirmRecharge() {
    if (!this.data.inpatientInfo) {
      wx.showToast({
        title: '住院人信息缺失',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    if (this.data.selectedAmount <= 0) {
      wx.showToast({
        title: '请选择充值金额',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    // 显示支付确认信息
    this.setData({
      paymentAmount: this.data.selectedAmount,
      showPaymentModal: true
    });
  },

  // 关闭支付密码模态框
  closePaymentModal() {
    this.setData({
      showPaymentModal: false,
      paymentPassword: ''
    });
  },

  // 输入支付密码
  inputPaymentPassword(e) {
    const password = e.detail.value;
    this.setData({
      paymentPassword: password
    });
  },

  // 删除支付密码
  deletePaymentPassword() {
    let currentPassword = this.data.paymentPassword;
    if (currentPassword.length > 0) {
      currentPassword = currentPassword.slice(0, -1);
      this.setData({
        paymentPassword: currentPassword
      });
    }
  },

  // 验证支付密码
  verifyPayment() {
    const password = this.data.paymentPassword;
    
    if (password.length !== 6) {
      wx.showToast({
        title: '请输入6位支付密码',
        icon: 'error',
        duration: 1500
      });
      return;
    }

    // 模拟支付验证
    wx.showLoading({
      title: '充值中...',
      mask: true
    });

    setTimeout(() => {
      wx.hideLoading();
      
      // 模拟充值成功
      wx.showModal({
        title: '充值成功',
        content: `已成功为${this.data.inpatientInfo.name}充值${this.data.paymentAmount}元`,
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            // 跳转到充值成功页面
            wx.navigateTo({
              url: `/pages/inpatient-recharge-success/inpatient-recharge-success?amount=${this.data.paymentAmount}&inpatientName=${this.data.inpatientInfo.name}`
            });
          }
        }
      });

      this.closePaymentModal();
    }, 2000);
  },

          // 跳转到充值记录页面
        goToRechargeRecord() {
          wx.navigateTo({
            url: '/pages/recharge-history/recharge-history'
          });
        },

  // 阻止事件冒泡
  stopPropagation() {
    // 阻止事件冒泡
  },

  // 返回住院人员选择页面
  goBack() {
    wx.navigateBack();
  },

  onShow() {
    console.log('住院充值页面显示');
  }
});
